package com.mall.controller;

import com.mall.common.Result;
import com.mall.filter.UserFilter;
import com.mall.model.vo.CartVo;
import com.mall.service.CartService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class  CartController {
    @Autowired
    private CartService cartService;
//    不输入userId是为了防止用户横向越权
    @ApiOperation("添加购物车")
    @PostMapping("/cart/add")
    public Result add(@RequestParam("productId") Integer productId,
                      @RequestParam("count") Integer count){
//        用户id后台获取防止横向越权
//        为什么要用vo呢？因为vo和PO类似，但是抽象出的vo对象,可以不和表一一对应，只修改我想修改的内容不用全部都改。
    List<CartVo> list = cartService.addCart(UserFilter.currentUser.getId(),productId,count);
        return Result.success(list);
    }

    @ApiOperation("查看购物车列表")
    @GetMapping("/cart/list")
    public Result list(){
//        用户id后台获取防止横向越权
        List<CartVo> list = cartService.list(UserFilter.currentUser.getId());
        return Result.success(list);
    }

    @ApiOperation("更新购物车")
    @PostMapping("/cart/update")
    public Result update(@RequestParam("productId") Integer productId,@RequestParam("count") Integer count){
    List<CartVo> list= cartService.updateCart(UserFilter.currentUser.getId(),productId,count);
    return Result.success(list);
    }

    @ApiOperation("删除购物车购物项")
    @PostMapping("/cart/delete")
    public Result delete(@RequestParam("productId") Integer productId){
        cartService.deleteCart(UserFilter.currentUser.getId(),productId);
        return Result.success();

    }
    @ApiOperation("修改某个商品的选中状态")
    @PostMapping("/cart/select")
    public Result select(@RequestParam("productId") Integer productId,@RequestParam("selected") Integer selected){
        List<CartVo> list = cartService.select(UserFilter.currentUser.getId(),productId,selected);
        return Result.success(list);
    }

    @ApiOperation("修改所有商品的选中状态")
    @PostMapping("/cart/selectAll")
    public Result selectAll(@RequestParam("selected") Integer selected){
        List<CartVo> list = cartService.selectAll(UserFilter.currentUser.getId(),selected);
        return Result.success(list);
    }
}
